﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using Common.Logging;


//样品编号	Sample_ID	
//产品型号名称	Model	
//出厂编号	EXFactory_ID	
//出厂日期	ExFactory_Date	
//生产厂家	Producer	
//委检单位	Consign_Corp	
//送检单位	Deliver_Corp	
//技术条件	Tech_Condition	
//试验大纲	Test_Outline	
//登记日期	Reg_Date	
//额定电压	Rated_V	
//额定电流	Rated_A	
//耐受电流	Resist_A
//峰值电流	Apex_A
//委托方代表	Commissary
//创建时间	Create_Time
//最后修改时间	Update_Time

public partial class Sample_Parameter : System.Web.UI.Page
{
    private DaSampleParameter dalSample = DevFuture.Common.WorkContext.AppCtx.GetObject("DaSampleParameter") as DaSampleParameter;
    private UserContext context;
    private DaTfiles dalTfile = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTfiles") as DaTfiles;
    private static readonly ILog log = LogManager.GetCurrentClassLogger();

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            this.BtnQuery_Click(null, null);

            this.SetPanelVisiable(SummaryPanel);

            if (Request.QueryString["sampleid"] != null && !string.IsNullOrEmpty(Request.QueryString["sampleid"].ToString()))
            {
                this.DisplaySampleEditInfo(Request.QueryString["sampleid"].ToString());
                this.SetPanelVisiable(EditPanel);
            }
        }
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoSampleParameter mod = new MoSampleParameter();

        mod.SampleId = EditSample_ID.Text;

        mod.ApexA = decimal.Parse(EditApex_A.Text.Trim());
        mod.Commissary = EditCommissary.Text.Trim();
        mod.ConsignCorp = EditConsign_Corp.Text.Trim();
        mod.CreateTime = DateTime.Now;
        mod.DeliverCorp = EditDeliver_Corp.Text.Trim();
        mod.ExfactoryDate = DateTime.Parse(EditExFactory_Date.Text.Trim());
        mod.ExfactoryId = EditEXFactory_ID.Text.Trim();
        mod.Model = EditModel.Text.Trim();
        mod.Producer = EditProducer.Text.Trim();
        mod.RatedA = decimal.Parse(EditRated_A.Text.Trim());
        mod.RatedV = decimal.Parse(EditRated_V.Text.Trim());
        mod.RegDate = DateTime.Now;
        mod.ResistA = decimal.Parse(EditResist_A.Text.Trim());
        mod.TechCondition = EditTech_Condition.Text.Trim();
        mod.TestOutline = EditTest_Outline.Text.Trim();
        mod.UpdateTime = DateTime.Now;

        int rtn = dalSample.Update(mod);

        if (rtn == 0)
        {
            //给用户一个提示。
            this.BtnQuery_Click(null, null);
        }
        else
        {
            //给用户一个提示。
        }

    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoSampleParameter mod = new MoSampleParameter();

        mod.SampleId = SequenceKeys.GenerateSampleID();
        mod.ApexA = decimal.Parse(CreateApex_A.Text.Trim());
        mod.Commissary = CreateCommissary.Text.Trim();
        mod.ConsignCorp = CreateConsign_Corp.Text.Trim();
        mod.CreateTime = DateTime.Now;
        mod.DeliverCorp = CreateDeliver_Corp.Text.Trim();
        mod.ExfactoryDate = DateTime.Parse(CreateExFactory_Date.Text.Trim());
        mod.ExfactoryId = CreateEXFactory_ID.Text.Trim();
        mod.Model = CreateModel.Text.Trim();
        mod.Producer = CreateProducer.Text.Trim();
        mod.RatedA = decimal.Parse(CreateRated_A.Text.Trim());
        mod.RatedV = decimal.Parse(CreateRated_V.Text.Trim());
        mod.RegDate = DateTime.Now;
        mod.ResistA = decimal.Parse(CreateResist_A.Text.Trim());
        mod.TechCondition = CreateTech_Condition.Text.Trim();
        mod.TestOutline = CreateTest_Outline.Text.Trim();
        mod.UpdateTime = DateTime.Now;

        int rtn = dalSample.Add(mod);

        if (rtn == 0)
        {
            //给用户一个提示。
            this.BtnQuery_Click(null, null);
        }
        else
        {
            //给用户一个提示。
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strSample_ID = DetailSample_ID.Text.Trim();
        this.DisplaySampleEditInfo(strSample_ID);
        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        string strSample_ID = DetailSample_ID.Text.Trim();
        this.DeleteSampleInfo(strSample_ID);
        this.BtnQuery_Click(null, null);
        //这里要给用户一个删除结果提示。
    }

    protected void GvDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strSample_ID = gvr.Cells[0].Text.Trim();

        this.DeleteSampleInfo(strSample_ID);
        this.BtnQuery_Click(null, null);
    }

    protected void GvEdit_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strSample_ID = gvr.Cells[0].Text.Trim();

        this.DisplaySampleEditInfo(strSample_ID);
        this.SetPanelVisiable(EditPanel);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strSample_ID = gvr.Cells[0].Text.Trim();

        this.DisplaySampleInfo(strSample_ID);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        Thread.Sleep(3000);
        //这里查询样品信息并绑定到gridview上。
        string strCondition = "where 1=1 ";

        if (!string.IsNullOrEmpty(TxtSample_ID.Text.Trim()))
        {
            strCondition = strCondition + "and  Sample_ID='" + TxtSample_ID.Text.Trim() + "'";
        }

        if (!string.IsNullOrEmpty(TxtModel.Text.Trim()))
        {
            strCondition = strCondition + "and  Model like '%" + TxtModel.Text.Trim() + "%'";
        }

        this.SampleGridView.DataSource = dalSample.Query(strCondition);
        SampleGridView.DataBind();

        this.SetPanelVisiable(SummaryPanel);
    }

    protected void DeleteSampleInfo(string strSample_ID)
    {
        //执行删除操作。
        int rtn = dalSample.DeleteByWhereClause(String.Format(" WHERE SAMPLE_ID='{0}'", strSample_ID));
        if (rtn == 0)
        {
            //成功 
        }
        else
        {
            //失败
        }
    }

    protected void DisplaySampleInfo(string strSample_ID)
    {
        //这里查询样品信息
        MoSampleParameter mod = new MoSampleParameter();
        mod.SampleId = strSample_ID;
        dalSample.GetEntityEx(ref mod);

        //然后给查看视图赋值。
        DetailSample_ID.Text = mod.SampleId.ToString();
        DetailModel.Text = mod.Model;
        DetailEXFactory_ID.Text = mod.ExfactoryId;
        DetailExFactory_Date.Text = mod.ExfactoryDate.ToString("yyyy-MM-dd");
        DetailProducer.Text = mod.Producer;
        DetailConsign_Corp.Text = mod.ConsignCorp;
        DetailDeliver_Corp.Text = mod.DeliverCorp;
        DetailTech_Condition.Text = mod.TechCondition;
        DetailTest_Outline.Text = mod.TestOutline;
        DetailRated_V.Text = mod.RatedV.ToString();
        DetailRated_A.Text = mod.RatedA.ToString();
        DetailResist_A.Text = mod.ResistA.ToString();
        DetailApex_A.Text = mod.ApexA.ToString();
        DetailCommissary.Text = mod.Commissary;
        DetailReg_Date.Text = mod.RegDate.ToString("yyyy-MM-dd");
        DetailUpdate_Time.Text = mod.UpdateTime.ToString("yyyy-MM-dd");
    }

    protected void DisplaySampleEditInfo(string strSample_ID)
    {
        //这里查询样品信息
        MoSampleParameter mod = new MoSampleParameter();
        mod.SampleId = strSample_ID;
        dalSample.GetEntityEx(ref mod);

        btnAddAttachment.NavigateUrl = "uploadfiles.aspx?sample&id=" + strSample_ID + "&m=0";

        //然后给编辑视图赋值。
        EditSample_ID.Text = mod.SampleId.ToString();
        EditModel.Text = mod.Model;
        EditEXFactory_ID.Text = mod.ExfactoryId;
        EditExFactory_Date.Text = mod.ExfactoryDate.ToString("yyyy-MM-dd");
        EditProducer.Text = mod.Producer;
        EditConsign_Corp.Text = mod.ConsignCorp;
        EditDeliver_Corp.Text = mod.DeliverCorp;
        EditTech_Condition.Text = mod.TechCondition;
        EditTest_Outline.Text = mod.TestOutline;
        EditRated_V.Text = mod.RatedV.ToString();
        EditRated_A.Text = mod.RatedA.ToString();
        EditResist_A.Text = mod.ResistA.ToString();
        EditApex_A.Text = mod.ApexA.ToString();
        EditCommissary.Text = mod.Commissary;

    }

    protected void CreateCancel()
    {
        CreateSample_ID.Text = "";
        CreateModel.Text = "";
        CreateEXFactory_ID.Text = "";
        CreateExFactory_Date.Text = "";
        CreateProducer.Text = "";
        CreateConsign_Corp.Text = "";
        CreateDeliver_Corp.Text = "";
        CreateTech_Condition.Text = "";
        CreateTest_Outline.Text = "";
        CreateRated_V.Text = "";
        CreateRated_A.Text = "";
        CreateResist_A.Text = "";
        CreateApex_A.Text = "";
        CreateCommissary.Text = "";
    }


    protected void RepFileEidt_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strKey = e.CommandName;
        try
        {
            dalTfile.DeleteOneAttachFile(strKey);
            this.DisplaySampleEditInfo(strKey);
            OperationResult.Text = "删除附件成功!"; divResult.Visible = true;
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "删除附件失败!"; divResult.Visible = true;
        }
    }
}
